package tables

import (
	"fmt"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"time"

	"gorm.io/gorm"
)

type MaskWords struct { //开始结束视频
	Model
	Rid               int64  `json:"rid" gorm:"type:bigint(20);unsigned;not null;default:0;comment:real_question表的唯一id"`
	AnswerAudioOrigin string `json:"answer_audio_origin" gorm:"type:varchar(255);not null;default:\"\";comment:回答音频地址"`
	AnswerAudioMask   string `json:"answer_audio_mask" gorm:"type:varchar(255);not null;default:\"\";comment:处理后的音频地址"`
	AnswerMaskWords   string `json:"answer_mask_words" gorm:"type:varchar(1024);not null;default:\"\";comment:被处理的词"`
	AnswerMaskIds     string `json:"answer_mask_ids" gorm:"type:varchar(255);not null;default:\"\";comment:被处理的词索引下标"`
	AnswerText        string `json:"answer_text" gorm:"type:longtext;not null;comment:回答的文本"`
	AnswerTextMask    string `json:"answer_text_mask" gorm:"type:varchar(1024);not null;default:\"\";comment:被处理后的文本"`
}

func (o *MaskWords) TableName() string {
	return "mask_words"
}

// 根据条件查询maskwords
func (o *MaskWords) GetMaskWordsByWhere(whereMap map[string]interface{}, where string, odr string, page int, size int) ([]MaskWords, int64, error) {
	var ol []MaskWords
	var count int64 = 0
	if page >= 1 && size > 0 {
		if len(odr) != 0 {
			if d := variable.DB.MySQL.Where(whereMap).Where(where).Offset((page - 1) * size).Limit(size).Order(odr).Find(&ol).Offset(-1).Limit(-1).Count(&count); d.Error != nil {
				return ol, count, d.Error
			}
		} else {
			if d := variable.DB.MySQL.Where(whereMap).Where(where).Offset((page - 1) * size).Limit(size).Find(&ol).Offset(-1).Limit(-1).Count(&count); d.Error != nil {
				return ol, count, d.Error
			}
		}
	} else if len(odr) != 0 {
		if d := variable.DB.MySQL.Where(whereMap).Where(where).Order(odr).Find(&ol).Limit(-1).Count(&count); d.Error != nil {
			return ol, count, d.Error
		}
	} else {
		if d := variable.DB.MySQL.Where(whereMap).Where(where).Find(&ol).Limit(-1).Count(&count); d.Error != nil {
			return ol, count, d.Error
		}
	}
	return ol, count, nil
}

// 根据Rid获取信息
func (*MaskWords) GetMaskWordsByRid(rid int64) (MaskWords, error) {
	var m MaskWords
	if err := variable.DB.MySQL.Where("rid=?", rid).Find(&m).Error; err != nil {
		return m, fmt.Errorf(errors.DatabaseMySqlOptionFail)
	}
	return m, nil
}

// 更新信息 sel 要更新的字段
func (m *MaskWords) UpdateMaskWords(id uint, maskwords MaskWords, sel ...string) error {
	if len(sel) == 0 {
		d := variable.DB.MySQL.Model(m).Where("id = ?", id).Updates(&maskwords)
		if d.RowsAffected == 0 {
			return d.Error
		}
	} else {
		d := variable.DB.MySQL.Model(m).Select(sel).Where("id = ?", id).Updates(&maskwords)
		if d.RowsAffected == 0 {
			return d.Error
		}
	}
	return nil
}

// 根据会员id更新失效
func (*MaskWords) DeleteMaskWordsById(id int64) error {
	var maskWords MaskWords
	maskWords.DeletedAt = gorm.DeletedAt{time.Now(), true}
	d := variable.DB.MySQL.Model(&maskWords).Select("deleted_at").Where("id = ?", id).Updates(&maskWords).Limit(1)
	return d.Error
}

func (*MaskWords) CreateMaskWords(m MaskWords) (uint, error) {
	d := variable.DB.MySQL.Create(&m)
	if d.RowsAffected == 0 {
		return 0, d.Error
	}
	return m.ID, nil
}
